﻿using PetkaWebDav.Server.Framework.BaseClasses;
using PetkaWebDav.Server.Framework.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PetkaWebDav.Handler.FileSystem.Handlers
{
    public sealed class DavGet : DavGetBase
    {
        public DavGet()
        {
            this.ProcessDavRequest += new DavProcessEventHandler(DavGet_ProcessDavRequest);
        }

        private void DavGet_ProcessDavRequest(object sender, EventArgs e)
        {
            FileInfo _fileInfo = RequestWrapper.GetFile(base.RelativeRequestPath);
            if (_fileInfo == null)
                base.AbortRequest(ServerResponseCode.NotFound);
            else
            {
                using (FileStream _fileStream = _fileInfo.OpenRead())
                {
                    long _fileSize = _fileStream.Length;
                    byte[] _responseBytes = new byte[_fileSize];

                    _fileStream.Read(_responseBytes, 0, (int)_fileSize);

                    base.ResponseOutput = _responseBytes;

                    _fileStream.Close();
                }
            }
        }
    }
}
